﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OpenAdmin.Application.Services;
using OpenAdmin.Application.Services.Dtos;

namespace OpenAdmin.HttpApi.Controllers;

/// <summary>
/// 角色相关Api
/// </summary>
[ApiExplorerSettings(GroupName = "Application")]
public class RoleController : ApiControllerBase
{
    private readonly IRoleService _roleServices;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="roleServices"></param>
    public RoleController(IRoleService roleServices)
    {
        _roleServices = roleServices;
    }
    /// <summary>
    /// 获取全部角色列表
    /// </summary>
    /// <returns></returns>
    [HttpGet, ProducesResponseType(typeof(List<RoleResponseDto>), 200)]
    public async Task<IActionResult> GetRoles(string keyworld = default)
    {
        return Ok(await _roleServices.GetRoles(keyworld));
    }

    /// <summary>
    /// 分页获取角色列表
    /// </summary>
    /// <returns></returns>
    [HttpGet, ProducesResponseType(typeof(PagedList<RolesByPageResponseDto>), 200)]
    public async Task<IActionResult> GetRolesByPage([FromQuery] RolesByPageRequestDto requestDto)
    {
        return Ok(await _roleServices.GetRolesByPage(requestDto));
    }

    /// <summary>
    /// 获取用户角色列表
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    [HttpGet, ProducesResponseType(typeof(List<RoleResponseDto>), 200)]
    public async Task<IActionResult> GetRolesByUserId(int userId)
    {
        return Ok(await _roleServices.GetRolesByUserId(new int[] { userId }));
    }

    /// <summary>
    /// 添加角色
    /// </summary>
    /// <param name="requestDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IActionResult> AddRole(AddRoleRequestDto requestDto)
    {
        await _roleServices.AddRole(requestDto);

        return Ok();
    }

    /// <summary>
    /// 更新角色信息
    /// </summary>
    /// <param name="requestDto"></param>
    [HttpPost]
    public async Task<IActionResult> EditRole(EditRoleRequestDto requestDto)
    {
        await _roleServices.EditRole(requestDto);

        return Ok();
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="id"></param>
    [HttpPost]
    public async Task<IActionResult> DeleteRole(int id)
    {
        await _roleServices.DeleteRole(id);

        return Ok();
    }
}
